package com.dcy.znoa.controller.publiz;

import com.dcy.znoa.common.annotation.AuthenticationAPI;
import com.dcy.znoa.common.controller.BaseController;
import com.dcy.znoa.common.exception.BusinessException;
import com.dcy.znoa.common.util.pojo.JsonCommon;
import com.dcy.znoa.common.util.pojo.JsonForList;
import com.dcy.znoa.controller.publiz.model.camera.AddCameraModel;
import com.dcy.znoa.controller.publiz.model.camera.LogicDelCameraModel;
import com.dcy.znoa.controller.publiz.model.camera.SelectCameraModel;
import com.dcy.znoa.controller.publiz.model.camera.UpdateCameraModel;
import com.dcy.znoa.dao.publiz.CameraDao;
import com.dcy.znoa.domain.publiz.Camera;
import com.dcy.znoa.service.publiz.CameraService;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.lang.reflect.InvocationTargetException;

import static com.dcy.znoa.common.util.pojo.JsonCommon.*;

/**
 * @author pengyu
 */
//视频 controller
@Controller
@RequestMapping("camera")
public class CameraController extends BaseController {
    @Autowired
    CameraService cameraService;
    @Autowired
    CameraDao cameraDao;

    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/selectList")
    public JsonForList selectList(@Valid SelectCameraModel model, BindingResult bindingResult) throws BusinessException, InvocationTargetException, IllegalAccessException {
        if (bindingResult.hasErrors()){
            return  new JsonForList("404",bindingResult.getFieldError().getDefaultMessage());
        }
        return cameraService.selectList(model);
//        List list = cameraService.selectList(model);
//        if(list.isEmpty()){
//            return new JsonForList(NOT_FOUND_CODE);
//        }else{
//            return new JsonForList(list);
//        }
    }
    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/insert")
    public JsonCommon insert(@Valid AddCameraModel model, BindingResult bindingResult) throws BusinessException, InvocationTargetException, IllegalAccessException {
        if (bindingResult.hasErrors()){
            return  new JsonCommon("404",bindingResult.getFieldError().getDefaultMessage());
        }
        model.setCreateOperId(getCurrentUser().getOperId());
        Camera cp = new Camera();
        BeanUtils.copyProperties(cp, model);
        Integer result =  cameraService.insert(cp);
        if(result>0){
            return new JsonCommon(SUCCESS_CODE);
        }else{
            return new JsonCommon(INSERT_ERROR_CODE);
        }
    }

    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/updateById")
    public JsonCommon updateById(@Valid UpdateCameraModel model, BindingResult bindingResult) throws BusinessException, InvocationTargetException, IllegalAccessException {
        if (bindingResult.hasErrors()){
            return  new JsonCommon("404",bindingResult.getFieldError().getDefaultMessage());
        }
        model.setUpdateOperId(getCurrentUser().getOperId());
        Camera cp = new Camera();
        BeanUtils.copyProperties(cp, model);
        Integer result =  cameraService.updateById(cp);
        if(result>0){
            return new JsonCommon(SUCCESS_CODE);
        }else{
            return new JsonCommon(UPDATE_ERROR_CODE);
        }
    }

    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/valid")
    public JsonCommon valid(@Valid LogicDelCameraModel model, BindingResult bindingResult) throws BusinessException, InvocationTargetException, IllegalAccessException {
        if (bindingResult.hasErrors()){
            return  new JsonCommon("404",bindingResult.getFieldError().getDefaultMessage());
        }
        model.setUpdateOperId(getCurrentUser().getOperId());
        Camera cp = new Camera();
        BeanUtils.copyProperties(cp, model);
        Integer result =  cameraService.updateById(cp);
        if(result>0){
            return new JsonCommon(SUCCESS_CODE);
        }else{
            return new JsonCommon(DELETE_ERROR_CODE);
        }
    }

    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/delById")
    public JsonCommon delById(Long cameraId) throws BusinessException, InvocationTargetException, IllegalAccessException {
        if (cameraId==null){
            return  new JsonCommon("404","cameraId不能为空");
        }
        Integer result =  cameraDao.deleteById(cameraId);
        if(result>0){
            return new JsonCommon(SUCCESS_CODE);
        }else{
            return new JsonCommon(DELETE_ERROR_CODE);
        }
    }


}
